package com.manger.utils;

import com.manger.frame.AutoCaseFrame;
import com.manger.model.CaseInfo;
import com.manger.model.Project;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ProjectUtils {

   public static Map<DetNameEnum, Project> projectMap = new HashMap<DetNameEnum, Project>();

    public static void init() throws Exception{
        String configXmlPath = SystemConstant.getConfigDir() + "/config.xml";
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(configXmlPath)));
        StringBuffer stringBuffer = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null) {
            stringBuffer.append(line);
        }
        SAXReader saxReader = new SAXReader();
        Document doc = DocumentHelper.parseText(stringBuffer.toString());
        Element root = doc.getRootElement();
        for (Iterator i = root.elementIterator("project"); i.hasNext();) {
            Element projectElement = (Element) i.next();
            Project project = new Project();
            project.setDebtName(projectElement.elementText("debtName"));
            project.setIgnoreDir(projectElement.elementText("ignoreDir"));
            project.setIgnoreFiles(projectElement.elementText("ignoreFiles"));
            CaseInfo caseInfo  = new CaseInfo();
            BeanUtils.copyProperties(project, caseInfo);
            projectMap.put(DetNameEnum.geDetNameEnumByDesc(project.getDebtName()), project);
        }
    }

    public static void main(String[] args) throws Exception{
        ProjectUtils.init();
        System.out.println(projectMap.keySet().size());
    }
}
